/**
 * Copyright: Copyright (c) 2012
 * Company:深圳市海乐淘电子商务有限公司
 * @author lifuyu
 * @date 2012-7-20 上午9:53:49
 * @version V1.0
 */
package com.microcm.system.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.microcm.common.CUtils;
import com.microcm.system.cache.SystemCacheKeeper;
import com.microcm.system.entity.SysMenu;
import com.microcmframework.core.action.BaseAction;

/**
 * 提供公共数据的action，此ACTION的数据是无需登录即可获取的
 */
@Controller("sysDataAction")
public class SysDataAction extends BaseAction
{
	private static final long serialVersionUID = 1L;

	@Autowired
	private SystemCacheKeeper systemCacheKeeper;

	/**
	 * 加载省市区下拉框数据
	 * 
	 * @author：lifuyu
	 * @return
	 * @throws Exception
	 */
	public String loadRange() throws Exception
	{
		int type = Integer.parseInt(this.getParamValue("type"));
		String parent = this.getParamValue("parent");
		List<String[]> areals = this.systemCacheKeeper.getAreas(type, parent);
		if (CUtils.isEmpty(areals))
			return this.sendRespJsonValue(ERROR, "区域数据不存在！");
		else
			return this.sendRespJsonValue(SUCCESS, areals);
	}

	/**
	 * 从cache中加载系统菜单
	 * 
	 * @author：lifuyu
	 * @return
	 * @throws Exception
	 */
	public String loadMenuFromCache() throws Exception
	{
		Integer pmnid = new Integer(this.getParamValue("pmnid"));
		List<SysMenu> qryLs = this.systemCacheKeeper.getMenuByPmnid(pmnid);

		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		for (SysMenu menu : qryLs)
		{

			Map<String, Object> m = new HashMap<String, Object>();

			m.put("id", menu.getMnid());
			m.put("text", menu.getName());
			boolean isParent = this.systemCacheKeeper.getMenuByPmnid(menu.getMnid()) != null;
			if (isParent)
				m.put("state", "closed");
			else
				m.put("state", "open");

			Map<String, Object> attrM = new HashMap<String, Object>();
			attrM.put("isParent", isParent);
			attrM.put("parentCid", menu.getPmnid());
			m.put("attributes", attrM);
			resultList.add(m);
		}
		return this.sendRespJsonValue(SUCCESS, resultList);
	}
	
	/**
	 * 从cache中加载tag
	 * @return
	 */
	public String loadTags()
	{
		
		return null;
	}
}
